package com.demo.gateway.config;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import reactor.core.publisher.Mono;

@Configuration
public class RateLimitConfig {

    /**
     * 接口级限流Key生成器：根据请求方法+路径生成唯一标识
     * 例如：GET:/api/user/1 -> "GET:/api/user/1"
     */
    @Bean
    public KeyResolver apiKeyResolver() {
        return exchange -> {
            String method = exchange.getRequest().getMethodValue();
            String path = exchange.getRequest().getPath().value();
            return Mono.just(method + ":" + path);
        };
    }
}